package com.sky.controller.user;

import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")

public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/list")
    public Result<List<DishVO>> list(Long categoryId){
        //构造redis 中的key  规则 dish_分类id
        String key = "dish_"+categoryId;
        //擦讯redis是否存在菜品数据
        List<DishVO> dishVOList = (List<DishVO>)redisTemplate.opsForValue().get(key);
        if (dishVOList != null && dishVOList.size()>0){
            //如果存在  直接返回 无需擦徐数据库
            return Result.success(dishVOList);
        }
        //没有存在在擦讯数据库  并将数据缓存到redis中
      dishVOList =   dishService.user_selectByCategoryId(categoryId);
        //并将数据缓存到redis中
        redisTemplate.opsForValue().set(key,dishVOList);
      return Result.success(dishVOList);
    }




}
